<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class DateFormatter
{
	private $minute;
	private $hour;
	private $day;
	private $week;
	private $year;
	private $decade;
	private $century;
	private $millenium;

	public function __construct() {
		$this->minute = 60;
		$this->hour = $this->minute * 60;
		$this->day = $this->hour * 24;
		$this->week = $this->day * 7;
		$this->month = $this->day * 30; //TODO: add logic for 31-day months and february
		$this->year = $this->day * 365; //TODO: add logic for leap years
		$this->decade = $this->year * 10;
		$this->century = $this->decade * 10;
		$this->millenium = $this->century * 10;
	}

	public function format($date, $informal = false, $hourlevel = false, $fullmonth = false) {
		$dif = time() - strtotime($date);
		$future = $dif < 0;
		$absdif = abs($dif);

		//TODO: clean up this mess

		if(strtotime($date) <= strtotime('-10 years')) {
			$years = $this->get_number_past($date, 'year');
			$decades = floor($years / 10);
			return "$decades decennia geleden";
		}
		if(strtotime($date) >= strtotime('+10 years')) {
			$years = $this->get_number_future($date, 'year');
			$decades = floor($years / 10);
			return "over $decades decennia";
		}
		if(strtotime($date) <= strtotime('-1 year')) {
			$years = $this->get_number_past($date, 'year');
			return "$years jaar geleden";
		}
		if(strtotime($date) >= strtotime('+1 year')) {
			$years = $this->get_number_future($date, 'year');
			return "over $years jaar";
		}
		if(strtotime($date) <= strtotime('-1 month')) {
			$months = $this->get_number_past($date, 'month');
			return "$months maand geleden";
		}
		if(strtotime($date) >= strtotime('+1 month')) {
			$months = $this->get_number_future($date, 'month');
			return "over $months maand";
		}
		if(strtotime($date) <= strtotime('-1 week')) {
			$weeks = $this->get_number_past($date, 'week');
			return "$weeks weken geleden";
		}
		if(strtotime($date) >= strtotime('+1 week')) {
			$weeks = $this->get_number_future($date, 'week');
			return "over $weeks weken";
		}
		if(strtotime($date) <= strtotime('-1 day')) {
			$days = $this->get_number_past($date, 'day');
			return "$days dagen geleden";
		}
		if(strtotime($date) >= strtotime('+1 day')) {
			$days = $this->get_number_future($date, 'day');
			return "over $days dagen";
		}
		if(strtotime($date) <= strtotime('-1 hour')) {
			$hours = $this->get_number_past($date, 'hour');
			return "$hours uur geleden";
		}
		if(strtotime($date) >= strtotime('+1 hour')) {
			$hours = $this->get_number_future($date, 'hour');
			return "over $hours uur";
		}
		if(strtotime($date) <= strtotime('-1 minute')) {
			$minutes = $this->get_number_past($date, 'minute');
			return "$minutes minuten geleden";
		}
		if(strtotime($date) >= strtotime('+1 minute')) {
			$minutes = $this->get_number_future($date, 'minute');
			return "over $minutes minuten";
		}
		if(strtotime($date) <= strtotime('-1 second')) {
			$minutes = $this->get_number_past($date, 'second');
			return "$minutes seconden geleden";
		}
		if(strtotime($date) >= strtotime('+1 second')) {
			$minutes = $this->get_number_future($date, 'second');
			return "over $minutes seconden";
		}
	}

	private function get_number_past($date, $time_unit)
	{
		$i = 0; 
		while (strtotime($date) <= strtotime("-$i $time_unit")) {
			$i++;
		} 
		return $i - 1;
	}

	private function get_number_future($date, $time_unit)
	{
		$i = 0; 
		while (strtotime($date) > strtotime("+$i $time_unit")) {
			$i++;
		} 
		return $i;
	}

	public function real_format($then, $hourlevel) {
		return "{$then['day']}-{$then['month']}-{$then['year']}" . (($hourlevel >= 0) ? ", {$then['hour']}u{$then['min']}" : "");
	}
}
?>
